﻿using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using ZhiKeDuoServer.Model;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppCash
{
    public partial class frmrole : Form
    {

        Role rp = new Role();
        #region ------初始化变量------
        private string strSql = "";                 //查询条件
        private int intPage = 1;                    //当前页码
        private int intPageSize = 20;               //默认每页显示条数
        private int pageCounts = 0;                 //总计路数
        #endregion

        #region ------窗体实例化------
        public frmrole()
        {
            InitializeComponent();
        }
        #endregion

        #region ------窗体加载------
        private void frmrole_Load(object sender, EventArgs e)
        {
            gvList.AutoGenerateColumns = false;
            //设置DataGridView的显示样式
            this.Show();
            gvList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

            fillGVList("", intPageSize, 1);         //填充数据

            //分页
            //初始页面显示条数选择框
            for (int i = 1; i < 6; i++)
            {
                int j = i * 10;
                cbPageSize.Items.Add(j.ToString());
            }
            cbPageSize.Text = "20";                 //页面条数显示框默认显示为20条

            //绑定页面显示条数设置控件的TextChanged事件
            cbPageSize.TextChanged += new EventHandler(cbPageSize_TextChanged);

            showtree();

        }

        private void showtree(List<PriRole> p = null)
        {
            if (p == null)
            {
                p = AllRpi.Pris;
            }
            advTree1.Nodes.Clear();
            foreach (var item in p)
            {
                Node node = new Node();
                node.Text = item.Name;
                node.CheckBoxVisible = true;
                node.Checked = item.sel;
                // var node = advTree1.Nodes.Add(item.Name);

                foreach (var subs in item.Subs)
                {
                    Node subsitem = new Node(Name = subs.Name);
                    subsitem.CheckBoxVisible = true;
                    subsitem.Checked = subs.sel;
                    node.Nodes.Add(subsitem);
                }
                node.ExpandAll();
                advTree1.Nodes.Add(node);


            }
        }


        #endregion





        #region ------弹出添加窗口------
        private void btnAdd_Click(object sender, EventArgs e)
        {
            rp = new Role();
            showtree();

        }
        #endregion

        #region ------搜索------
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbKey.Text.Trim() != "")
            {
                string key = Maticsoft.Common.StringPlus.GetText(tbKey.Text);
                strSql = "Name like '%" + key + "%'";
                intPage = 1;
                fillGVList(strSql, intPageSize, 1);

            }
        }
        #endregion

        #region ------共用刷新方法------
        public void refreshData()
        {
            fillGVList(strSql, intPageSize, intPage);
        }
        #endregion

        #region ------填出datagridview------
        private void fillGVList(string key, int pageSize, int page)
        {
            strSql = key;
            intPage = page;
            ZhiKeDuoServer.BLL.RoleBll bVip = new ZhiKeDuoServer.BLL.RoleBll();

            var ds = bVip.GetListByPage(key, "", pageSize, page);

            gvList.DataSource = ds;
            //gvList.Columns[0].Visible = false;
            if (ds.Count > 0)
            {
                intPages();
            }
            else
            {
                MessageBoxEx.Show("对不起，没有您搜索的记录!");
            }

        }
        #endregion

        #region ------显示所有数据------
        private void btnAll_Click(object sender, EventArgs e)
        {
            fillGVList("", intPageSize, intPage);
            intPage = 1;


        }
        #endregion

        #region ------单条删除记录------
        private void btnDel_Click(object sender, EventArgs e)
        {
            //删除会员
            string id = gvList.SelectedRows[0].Cells[0].Value.ToString();
            if (id != "")
            {
                if (MessageBoxEx.Show("确定删除该记录？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    ZhiKeDuoServer.BLL.RoleBll bMember = new ZhiKeDuoServer.BLL.RoleBll();
                    bMember.Delete(int.Parse(id));
                    this.gvList.Rows[0].Selected = true;
                    fillGVList(strSql, intPageSize, intPage);
                }
            }
            else
            {
                MessageBoxEx.Show("请选择要删除的行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion



        #region ------双击行弹出编辑界面------
        private void gvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = gvList.SelectedRows[0].Cells[0].Value.ToString();
            ZhiKeDuoServer.BLL.RoleBll bVip = new ZhiKeDuoServer.BLL.RoleBll();
            rp = bVip.GetModel(Convert.ToInt32(id));
            roleName.Text = rp.Name;
            if (string.IsNullOrEmpty(rp.Exts))
            {
                showtree();
            }
            else
            {
                showtree(JsonConvert.DeserializeObject<List<PriRole>>(rp.Exts));
            }

        }
        #endregion

        #region ------分页代码------
        private void intPages()
        {
            ZhiKeDuoServer.BLL.RoleBll bVip = new ZhiKeDuoServer.BLL.RoleBll();
            int counts = bVip.GetRecordCount(strSql);
            lblCounts.Text = counts.ToString();
            if (counts % intPageSize == 0)
            {
                pageCounts = counts / intPageSize;

            }
            else
            {
                pageCounts = (counts / intPageSize) + 1;

            }

            cbPage.Items.Clear();
            for (int i = 1; i <= pageCounts; i++)
            {
                cbPage.Items.Add(i.ToString());
            }
            cbPage.SelectedIndex = intPage - 1;
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            //转移到第一页
            fillGVList(strSql, intPageSize, 1);

        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            intPage--;
            if (intPage >= 1)
            {
                fillGVList(strSql, intPageSize, intPage);
            }
            else
            {
                intPage++;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            intPage++;
            if (intPage <= pageCounts)
            {
                fillGVList(strSql, intPageSize, intPage);
            }
            else
            {
                intPage--;
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            fillGVList(strSql, intPageSize, pageCounts);
        }

        private void btnGoto_Click(object sender, EventArgs e)
        {
            int cPage = int.Parse(cbPage.Text);
            fillGVList(strSql, intPageSize, cPage);
        }

        private void cbPageSize_TextChanged(object sender, EventArgs e)
        {
            if (cbPageSize.Text != "")
            {
                int cPageSize = int.Parse(cbPageSize.Text);
                intPageSize = cPageSize;
                fillGVList(strSql, cPageSize, 1);
            }
        }






        #endregion



        #region 选择权限

        private void advTree1_NodeClick(object sender, TreeNodeMouseEventArgs e)

        {


            foreach (Node item in e.Node.Nodes)
            {
                item.Checked = e.Node.Checked;
            }

            bool issel = false;
            if (e.Node.Parent != null)
            {
                foreach (Node item in e.Node.Parent.Nodes)
                {
                    if (item.Checked)
                    {
                        issel = !false;
                        break;
                    }
                }

                e.Node.Parent.Checked = issel;
            }


        }


        #endregion

        private void buttonX1_Click(object sender, EventArgs e)
        {
            var AllRpis = new List<PriRole>();
            foreach (Node item in advTree1.Nodes)
            {
                PriRole pr = new PriRole();
                pr.Name = item.Text;
                pr.sel = item.Checked;
                foreach (Node button in item.Nodes)
                {

                    pr.Subs.Add(new PriRole { Name = button.Text, sel = button.Checked });
                }

                AllRpis.Add(pr);


            }
            string json = JsonConvert.SerializeObject(AllRpis);
            rp.Exts = json;
            ZhiKeDuoServer.BLL.RoleBll bVip = new ZhiKeDuoServer.BLL.RoleBll();
            if (roleName.Text.Trim() == "")
            {
                MessageBoxEx.Show("角色名称不能为空");
                return;
            }
            rp.Name = roleName.Text.Trim();
            if (rp.Id != 0)
            {
                bVip.Update(rp);
            }
            else
            {
                bVip.Add(rp);
            }
            refreshData();
        }
    }
}
